package com.qlm.tools;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

public class ZipCompressor {

private File zipFile;    
    
    /** 
     * 压缩文件构造函数 
     * @param pathName 最终压缩生成的压缩文件：目录+压缩文件名.zip 
     */  
    public ZipCompressor(String finalFile) {    
        zipFile = new File(finalFile);    
    }  
	
	public void compressExe(String srcPathName) {    
        File srcdir = new File(srcPathName);    
        if (!srcdir.exists()){  
            throw new RuntimeException(srcPathName + "不存在！");    
        }   
            
        Project prj = new Project();    
        Zip zip = new Zip();    
        zip.setProject(prj);    
        zip.setDestFile(zipFile);    
        FileSet fileSet = new FileSet();    
        fileSet.setProject(prj);    
        fileSet.setDir(srcdir);    
        //fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹 eg:zip.setIncludes("*.java");    
        //fileSet.setExcludes(...); //排除哪些文件或文件夹    
        zip.addFileset(fileSet);    
        zip.execute();    
    } 
	
	 public static void main(String[] args) { 
		 System.out.println("*******");
		 	String sourceFilePath = "E:\\sample\\sample\\java\\sign.java";
		 	String filePath = "";
		 	String fileName = "";
			String zipFilePath = "";
			File sourceFile = new File(sourceFilePath);
			if (sourceFile.exists()) {
				filePath = sourceFile.getParentFile().getAbsolutePath();
				fileName = sourceFile.getName();
				zipFilePath = sourceFile.getParentFile().getParentFile()
						.getAbsolutePath();
			}
//			System.out.println(filePath);
//			System.out.println(fileName);
//			System.out.println(zipFilePath);
//	        ZipCompressor zc = new  ZipCompressor(filePath+".zip");  
//	        zc.compressExe(filePath);  
	        System.out.println("------");
	    }
}
